package com.hnucm.cooklife.model;

import com.hnucm.cooklife.base.BaseModel;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.bean.me.DishColl;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.contract.mecollect.Contract;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public class MeCollectModel extends BaseModel implements Contract.IMeCollectModel {

    public MeCollectModel() {
        setCookies(false);
    }

    @Override
    public Observable<List<DishColl>> loadMeCollect(int userId) {
        return mApiServer.loadMyCollect(userId)
                .map(meCollectData -> {
                    List<DishColl> dishList = new ArrayList<>();
                    meCollectData.getData().stream().forEach(dataDTO -> {
//                        System.out.println("collect");
//                        System.out.println(dataDTO.toString());
                        DishColl dish = new DishColl();
                        dish.dishId = dataDTO.getDishId().getDishId();
                        dish.dishpic = dataDTO.getDishId().getDishpic();
                        dish.dishdone = dataDTO.getDishId().getDishdone();
                        dish.dishscore = dataDTO.getDishId().getDishscore();
                        dish.username = dataDTO.getDishId().getUsername();
                        dish.icon = dataDTO.getDishId().getIcon();
                        dish.userid = dataDTO.getDishId().getUserid();
                        dish.dishdesc = dataDTO.getDishId().getDishdesc();
                        dish.dishname = dataDTO.getDishId().getDishname();
                        dishList.add(dish);
                    });
                    return dishList;
                });
    }

    @Override
    public Observable<List<DishColl>> refreshMeCollect(int userId) {
        return loadMeCollect(userId);
    }

    @Override
    public Observable<ToAddCollectData> unCollect(int userId, int dishId) {
        return mApiServer.unCollect(userId,dishId);
    }
}
